<?php

class Position extends Controller {

	function Position()
	{
		parent::Controller();
		if(! $this->dx_auth->is_logged_in()	){
			redirect('/auth/login/', 'refresh');
		}
		
		$this->load->library('form_validation');
		$this->load->model('Positions','pos');
		$this->load->model('Candidates','cand');
		$this->load->model('ElectionEvent','event');
	}
	
	function show($name , $id)
	{
		
		$qry = $this->pos->get_pos_by_id($id);
		
			$var['pos'] = $qry->row();
			
		$qry2 = $this->cand->get_all_of_pos($var['pos']->id);
		
		$var['qryNum'] = $qry2->num_rows();
			
			if($var['qryNum'] <= 0){
				$var['result_msg'] = "No candidates";
			}else{
				$var['result_msg'] = "Showing ".$var['qryNum']." candidates";
			}
		
		$var['candArr'] = $qry2;
		
		$qry3 = $this->event->get_event_by_id($var['pos']->event_id);
			
			$var['event'] = $qry3->row()->event_name;
			
		$var['title_for_layout'] = "Position";
		$this->layout->view('positions/show',$var);
	}
	
	function newPos($name, $id){
		

			$var['default_event'] = $id;
			$var['title_for_layout'] = "New Position";
			$var['eventList'] = $this->_getEvent();
			$this->layout->view('positions/new', $var);

	}
	
	function submit(){
	
		$this->form_validation->set_rules('pos_name','Pos_name','required|max_length[45]');			
		$this->form_validation->set_rules('pos_desc','Pos_desc','required|max_length[200]');			
		$this->form_validation->set_rules('event_id','event_id','required|max_length[11]');			
		$this->form_validation->set_rules('pos_win_votes','pos_win_votes','required|is_numeric|max_length[11]');			
		$this->form_validation->set_rules('order','order','required|is_numeric|max_length[11]');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn'\t been passed
		{
			$var['default_event'] = $id;
			$var['title_for_layout'] = "New Position";
			$var['eventList'] = $this->_getEvent();
			$this->layout->view('positions/new', $var);
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			
			$form_data = array(
					       	'pos_name' => set_value('pos_name'),
					       	'pos_desc' => set_value('pos_desc'),
					       	'event_id' => set_value('event_id'),
					       	'pos_win_votes' => set_value('pos_win_votes'),
					       	'order' => set_value('order')
						);
					
			// run insert model to write data to db
		
			if ($this->pos->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('events/show/event_id/'.$form_data['event_id']);   // or whatever logic needs to occur
			}
			else
			{
			echo 'An error occurred saving your information. Please try again later';
			// Or whatever error handling is necessary
			}
		}
			
	}
	
	function _getEvent(){
		
		$qry = $this->event->get_all();
		$arr = array();
		
		foreach( $qry->result() as $row ){
			$arr[$row->id] = $row->event_name;	
		}
		
		return $arr;
			
	}
		
	}


/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */